﻿'*************************************************************************
'@Tác giả : Dương Văn Thỉnh
'@Tên file: FrmDMQuyen.vb
'@Ngày tạo: 08/12/2009
'@Ngày sửa: 08/12/2009
'@Mục đích: Danh mục quyền
'*************************************************************************
'vFrmDMQuyen  Danh mục quyền
'eFrmDMQuyen  Danh muc quyen
Imports Publics
Imports DevExpress.XtraTreeList

Public Class FrmDMQuyen

    Public IsActive As Boolean = False
    Private vSave As TreeListViewState
    Private Sub FillDataToTree()
        vTreeDMQuyen.DataSource = From ds In dbTV.DM_Quyens _
                                  Where ds.InUsed _
                                  Order By ds.ThuTu _
                                  Select ds.ID, ds.ParentID, ds.TenQuyen, ds.MaQuyen
    End Sub

    Private Sub FrmDMQuyen_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        IsActive = False
    End Sub

    Private Sub FrmDMQuyen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BtnThemMoi.Caption = BtnAddNewCaption
        BtnSuaDoi.Caption = BtnEditCaption
        BtnXoa.Caption = BtnDeleteCaption
        BtnLamTuoi.Caption = BtnRefreshCaption
        BtnXuatDuLieu.Caption = BtnExportDataCaption
        btnTroGiup.Caption = BtnHelpCaption
        btnHuyBo.Caption = BtnCancelCaption
        Me.Text = "Danh mục quyền"
        IsActive = True
        FillDataToTree()
        vTreeDMQuyen.ExpandAll()
        If Not vUser.CanBo.UserName.ToLower = "superadmin" Then
            btnXuatDuLieuTSQL.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
    End Sub

    Private Sub BtnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnThemMoi.ItemClick, BarLargeButtonItem1.ItemClick
        Dim vFrmCapNhatDMQ = New FrmCapNhatDMQuyen
        vFrmCapNhatDMQ.ActionForm = vAddAction
        vFrmCapNhatDMQ.ShowDialog()
        vSave = New TreeListViewState(vTreeDMQuyen)
        vSave.SaveState()
        FillDataToTree()
        vSave.LoadState()
    End Sub

    Private Sub BtnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSuaDoi.ItemClick, BarLargeButtonItem2.ItemClick
        Dim vEditPermission As DM_Quyen = (From ds In dbTV.DM_Quyens _
                                          Where ds.ID.Equals(Me.vTreeDMQuyen.FocusedNode.GetValue(vTreeColumnID)) _
                                          Select ds).FirstOrDefault()
        If vEditPermission IsNot Nothing Then
            Dim vFrmCapNhatDMQ = New FrmCapNhatDMQuyen
            vFrmCapNhatDMQ.ActionForm = vEditAction
            vFrmCapNhatDMQ.objDMQ = vEditPermission
            vFrmCapNhatDMQ.ShowDialog()
            If vFrmCapNhatDMQ.IDQuyen <> "" Then
                vSave = New TreeListViewState(vTreeDMQuyen)
                vSave.SaveState()
                FillDataToTree()
                vSave.LoadState()
            End If
        End If
    End Sub

    Private Sub btnCopy_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCopy.ItemClick
        Dim vEditPermission As DM_Quyen = (From ds In dbTV.DM_Quyens _
                                         Where ds.ID.Equals(Me.vTreeDMQuyen.FocusedNode.GetValue(vTreeColumnID)) _
                                         Select ds).FirstOrDefault()
        If vEditPermission IsNot Nothing Then
            Dim vFrmCapNhatDMQ = New FrmCapNhatDMQuyen
            vFrmCapNhatDMQ.ActionForm = vCopyAction
            vFrmCapNhatDMQ.objDMQ = vEditPermission
            vFrmCapNhatDMQ.ShowDialog()
            If vFrmCapNhatDMQ.IDQuyen <> "" Then
                vSave = New TreeListViewState(vTreeDMQuyen)
                vSave.SaveState()
                FillDataToTree()
                vSave.LoadState()
            End If
        End If
    End Sub

    Private Sub BtnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnXoa.ItemClick, BarLargeButtonItem3.ItemClick
        If Me.vTreeDMQuyen.FocusedNode IsNot Nothing Then
            If Me.vTreeDMQuyen.FocusedNode.HasChildren Then
                MsgErr("Bạn chưa chọn quyền cấp con")
                Exit Sub
            End If
            If MsgAskDelete("Bạn có muốn xóa bản ghi này không?") = Windows.Forms.DialogResult.Yes Then
                Dim vDeletePermission As DM_Quyen = (From ds In dbTV.DM_Quyens _
                                                           Where ds.ID.Equals(CStr(Me.vTreeDMQuyen.FocusedNode.GetValue(vTreeColumnID))) _
                                                           Select ds).FirstOrDefault()
                If vDeletePermission IsNot Nothing Then
                    vDeletePermission.InUsed = False
                    vDeletePermission.EditedOn = CDate(dbTV.GetDateTimeServer())
                    iSubmitChange()
                    vSave = New TreeListViewState(vTreeDMQuyen)
                    vSave.SaveState()
                    FillDataToTree()
                    vSave.LoadState()
                End If
            End If
        End If
    End Sub

    Private Sub BtnLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnLamTuoi.ItemClick, BarLargeButtonItem4.ItemClick
        FillDataToTree()
    End Sub



    Private Sub vTreeDMQuyen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles vTreeDMQuyen.KeyDown
        If vTreeDMQuyen.FocusedNode IsNot Nothing Then
            If e.KeyCode = Windows.Forms.Keys.Left Then
                Me.vTreeDMQuyen.FocusedNode.Expanded = False
            ElseIf e.KeyCode = Windows.Forms.Keys.Right Then
                Me.vTreeDMQuyen.FocusedNode.Expanded = True
            ElseIf e.KeyCode = Windows.Forms.Keys.Delete Then
                BtnXoa_ItemClick(Nothing, Nothing)
            End If
        End If
    End Sub

    Private Sub FrmDMQuyen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Thêm mới
        If (e.Modifiers = GetKeyModifide(pIdThem) And _
            e.KeyCode = GetKeyCode2(pIdThem)) Or _
            (e.KeyCode = GetKeyCode(pIdThem)) Then

            If BtnThemMoi.Enabled = True And BtnThemMoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnThemMoi_ItemClick(Nothing, Nothing)
            End If
        End If
        'Sửa
        If (e.Modifiers = GetKeyModifide(pIdSua) And _
           e.KeyCode = GetKeyCode2(pIdSua)) Or _
           (e.KeyCode = GetKeyCode(pIdSua)) Then

            If BtnSuaDoi.Enabled = True And BtnSuaDoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnSuaDoi_ItemClick(Nothing, Nothing)
            End If
        End If
        'Xoá
        If (e.Modifiers = GetKeyModifide(pIdXoa) And _
          e.KeyCode = GetKeyCode2(pIdXoa)) Or _
          (e.KeyCode = GetKeyCode(pIdXoa)) Then

            If BtnXoa.Enabled = True And BtnXoa.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnXoa_ItemClick(Nothing, Nothing)
            End If

        End If

        ''Xuất dữ liệu
        'If (e.Modifiers = GetKeyModifide(pIdXuat) And _
        '  e.KeyCode = GetKeyCode2(pIdXuat)) Or _
        '  (e.KeyCode = GetKeyCode(pIdXuat)) Then

        '    If BtnXuatDuLieu.Enabled = True And BtnXuatDuLieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
        '        BtnXuatDuLieu_ItemClick(Nothing, Nothing)
        '    End If

        'End If

        'Làm tươi REFESH
        If (e.Modifiers = GetKeyModifide(pIdLamTuoi) And _
          e.KeyCode = GetKeyCode2(pIdLamTuoi)) Or _
          (e.KeyCode = GetKeyCode(pIdLamTuoi)) Then

            If BtnLamTuoi.Enabled = True And BtnLamTuoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnLamTuoi_ItemClick(Nothing, Nothing)
            End If
        End If

        'Huỷ bỏ (Thoát)
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
             e.KeyCode = GetKeyCode2(pIdThoat)) Or _
             (e.KeyCode = GetKeyCode(pIdThoat)) Then
            btnHuyBo_ItemClick(Nothing, Nothing)
        End If
        'ExpandAll - CollapseAll
        If e.Modifiers = Windows.Forms.Keys.Control AndAlso e.KeyCode = Windows.Forms.Keys.A Then
            vTreeDMQuyen.ExpandAll()
        ElseIf e.Modifiers = Windows.Forms.Keys.Control AndAlso e.KeyCode = Windows.Forms.Keys.X Then
            vTreeDMQuyen.CollapseAll()
        End If
    End Sub

    Private Sub vTreeDMQuyen_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles vTreeDMQuyen.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            vPopupMenu.ShowPopup(New System.Drawing.Point(MousePosition.X, MousePosition.Y))
        End If
    End Sub


    Private Sub btnHuyBo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnHuyBo.ItemClick
        Me.Close()
    End Sub

    Private Sub btnXuatDuLieuDangHienThi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatDuLieuDangHienThi.ItemClick
        Dim vTreeListExport As TreeList = Me.vTreeDMQuyen
        Export_Dialog(Me.SaveFileDialog, vTreeListExport)
    End Sub

    Private Sub btnXuatDuLieuTSQL_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatDuLieuTSQL.ItemClick
        Dim vEditPermission As DM_Quyen = (From ds In dbTV.DM_Quyens _
                                          Where ds.ID.Equals(Me.vTreeDMQuyen.FocusedNode.GetValue(vTreeColumnID)) _
                                          Select ds).FirstOrDefault()
        If vEditPermission IsNot Nothing Then
            Dim lstPer = (From ds In dbTV.DM_Quyens Where ds.InUsed AndAlso ds.ParentID = vEditPermission.ID Select ds).ToList
            Dim strSql As String = ""
            strSql = String.Format("IF NOT EXISTS(SELECT * FROM DM_Quyen dq WHERE dq.MaQuyen = '{0}')", vEditPermission.MaQuyen)
            strSql &= vbCrLf
            strSql &= "INSERT INTO DM_Quyen(MaQuyen,TenQuyen,ThuTu,EditedOn,InUsed,IsSent,MacReceive,ID,ParentID)"
            strSql &= vbCrLf
            strSql &= String.Format("VALUES('{0}',N'{1}',{2},GETDATE(),1,0,NULL,'{3}','{4}')", vEditPermission.MaQuyen, vEditPermission.TenQuyen, vEditPermission.ThuTu, vEditPermission.ID, vEditPermission.ParentID)
            strSql &= vbCrLf
            strSql &= "GO"

            For Each item In lstPer
                strSql &= vbCrLf
                strSql &= String.Format("IF NOT EXISTS(SELECT * FROM DM_Quyen dq WHERE dq.MaQuyen = '{0}')", item.MaQuyen)
                strSql &= vbCrLf
                strSql &= "INSERT INTO DM_Quyen(MaQuyen,TenQuyen,ThuTu,EditedOn,InUsed,IsSent,MacReceive,ID,ParentID)"
                strSql &= vbCrLf
                strSql &= String.Format("VALUES('{0}',N'{1}',{2},GETDATE(),1,0,NULL,'{3}','{4}')", item.MaQuyen, item.TenQuyen, item.ThuTu, item.ID, item.ParentID)
                strSql &= vbCrLf
                strSql &= "GO"
            Next
            If strSql.Length > 0 Then
                System.IO.File.WriteAllText("D:\tmp.txt", strSql)
                System.Diagnostics.Process.Start("D:\tmp.txt")
            End If
        End If
    End Sub
End Class